//
//  SearchViewController.m
//  Travelling Together
//
//  Created by lanou3g on 15/11/30.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchCollectionViewCell.h"
#import "Single.h"

@interface SearchViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)UITextField *tf;
@property(nonatomic, strong)UISegmentedControl * sg;
@property(nonatomic, strong)NSArray *arrayCountry;
@property(nonatomic, strong)NSArray *arrayCity;
@property(nonatomic, strong)NSArray *arrayMonth;
@property(nonatomic, strong)UICollectionView * myCollectionView;

@end

@implementation SearchViewController
-(UICollectionView *)myCollectionView{
    if (_myCollectionView==nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(150, 50);
        
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
       _myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _myCollectionView.backgroundColor =[UIColor whiteColor];
       

    }
    return _myCollectionView;
}
-(NSArray*)arrayCountry{
    if (_arrayCountry==nil) {
        _arrayCountry = @[@"新加坡",@"日本",@"泰国",@"韩国",@"美国",@"法国",@"马来西亚",@"意大利",@"英国",@"德国",@"菲律宾",@"瑞士"];
    }
    return _arrayCountry;
}

-(NSArray*)arrayCity{
    if (_arrayCity==nil) {
        _arrayCity = @[@"台湾",@"香港",@"北京",@"广东",@"上海",@"云南",@"四川",@"浙江",@"福建",@"江苏",@"济南",@"武汉"];
    }
    return _arrayCity;
}
-(NSArray*)arrayMonth{
    if (_arrayMonth==nil) {
        _arrayMonth = @[@"一月",@"二月",@"三月",@"四月",@"五月",@"六月",@"七月",@"八月",@"九月",@"十月",@"十一月",@"十二月"];
    }
    return _arrayMonth;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"搜索";
    
    self.view.backgroundColor = [UIColor grayColor];

    self.tf = [[UITextField alloc] initWithFrame:CGRectMake(30, 66, self.view.frame.size.width - 20, 30)];
    self.tf.placeholder = @"搜索";
    [self.view addSubview:self.tf];
    
   

    
    NSArray * arr = @[@"国外",@"国内",@"月份"];
    self.sg = [[UISegmentedControl alloc] initWithItems:arr];
    self.sg.frame = CGRectMake(5, 99, self.view.frame.size.width-10, 30);
    
    
    self.sg.selectedSegmentIndex = 0;
    // UIControlEventValueChanged
    [self.sg addTarget:self action:@selector(clickAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:self.sg];
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.frame = CGRectMake(0, 136, self.view.frame.size.width, self.view.frame.size.height);
    
    [self.myCollectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
     [self.view addSubview:self.myCollectionView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- UICollectionView 协议方法
// 几个分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
    
}
// 每个分区有多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    switch (self.sg.selectedSegmentIndex) {
        case 0:
            cell.lb.text =self.arrayCountry[indexPath.row];
            cell.lb.textAlignment = NSTextAlignmentCenter;
            break;
        
        case 1:
            cell.lb.text =self.arrayCity[indexPath.row];
            cell.lb.textAlignment = NSTextAlignmentCenter;
            break;
        case 2:
            cell.lb.text =self.arrayMonth[indexPath.row];
            cell.lb.textAlignment = NSTextAlignmentCenter;
            break;
        default:
            break;
    }

    return cell;
    
}




// 点击事件
-(void)clickAction:(UISegmentedControl *)sender
{
    
    NSInteger index = [self.sg selectedSegmentIndex];
    switch (index) {
        case 0:
            [self.myCollectionView reloadData];
            break;
        case 1:
            NSLog(@"1");
            [self.myCollectionView reloadData];
            break;
        case 2:
            NSLog(@"2");[self.myCollectionView reloadData];
            break;
        default:
            break;
    }

    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"123");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
